(function ($) {
    $.fn.livedemo = function (options) {

        var codeblock = $('<div class="code-block"></div>');
        var blockclose = $('<span class="block-close">x</span>');

        return this.each(function () {

            var livedemo = $(this);

            livedemo.on('click', '.btn-code', function (e) {
                e.preventDefault();

                var btn = $(this);
                var block = $(this).parents('.demo-block');
                var codes = block.find('.demo-code').html();

                if (block.next().hasClass('code-block')) {
                    block.next().stop().slideToggle();
                } else {
                    livedemo.find('.btn-code').removeClass('filled');
                    livedemo.find('.code-block').stop().slideUp(function () {
                        $(this).remove()
                    });

                    var newblock = codeblock.clone();
                    newblock.append(codes).append(blockclose).insertAfter(block).slideDown();
                }

                $(this).toggleClass('filled');

            }).on('click', '.block-close', function () {
                $(this).parents('.code-block').stop().slideUp().prev('.demo-block').find('.btn-code').removeClass('filled');
            });

        });

    };
})(jQuery);

(function ($) {
    //拖拽插件,参数:id或object
    $.Move = function (_this, direction, stop_offset, opacity) {
        if (direction == null) direction = 'all';
        if (stop_offset == null) stop_offset = 0;
        if (opacity == null) opacity = 0.3
        if (typeof(_this) == 'object') {
            _this = _this;
        } else {
            _this = $("#" + _this);
        }
        if (!_this) {
            return false;
        }

        _this.css({'position': 'absolute'}).hover(function () {
            $(this).css("cursor", "move");
        }, function () {
            $(this).css("cursor", "default");
        })
        _this.mousedown(function (e) {//e鼠标事件
            var offset = $(this).offset();
            var x = e.pageX - offset.left;
            var y = e.pageY - offset.top;
            _this.css({'opacity': opacity});
            $(document).bind("mousemove", function (ev) {//绑定鼠标的移动事件，因为光标在DIV元素外面也要有效果，所以要用doucment的事件，而不用DIV元素的事件
                _this.bind('selectstart', function () {
                    return false;
                });
                var _x = ev.pageX - x;//获得X轴方向移动的值
                var _y = ev.pageY - y;//获得Y轴方向移动的值
                if (direction == 'all')
                    _this.css({'left': _x + "px", 'top': _y + "px"});
                else if (direction == 'x') {
                    if (stop_offset > 0 && _x > stop_offset)
                        _x = 0;
                    _this.css({'left': _x + "px"});
                } else if (direction == 'y') {
                    if (stop_offset > 0 && _y > stop_offset)
                        _y = 0;
                    _this.css({'top': _y + "px"});
                }
            });
        });

        $(document).mouseup(function () {
            $(this).unbind("mousemove");
            _this.css({'opacity': ''});
        })
    };
})(jQuery);

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function niceScroll(layout) {
    layout.niceScroll({boxzoom: false, cursorborder: "", cursorcolor: "#1d2d43"});
}

function initLocalCamera() {
    if (navigator.mediaDevices === undefined) {
        navigator.mediaDevices = {};
    }
    if (navigator.mediaDevices.getUserMedia === undefined) {
        navigator.mediaDevices.getUserMedia = function (constraints) {
            var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
            if (!getUserMedia) {
                return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
            }
            return new Promise(function (resolve, reject) {
                getUserMedia.call(navigator, constraints, resolve, reject);
            });
        }
    }
}

function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

function guid2() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
